package cn.kailangtu.dataspot.total;

import android.os.SystemClock;
import android.util.Log;
import org.greenrobot.eventbus.EventBus;
import org.litepal.LitePal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import cn.kailangtu.commonevents.eventEntity.TotalMainDataChangedEvent;
import cn.kailangtu.commonevents.eventEntity.TotalPopDataChangedEvent;
import cn.kailangtu.dataspot.db.NewTotalDataEntity;
import cn.kailangtu.dataspot.db.TotalDataEntity;

public  class TotalData {



    private static String TAG = "SSSSSSSSSSSSSSSS";
    public static int[] voltageValues;
    public static int[] electricValues;


    public static List<Long> lsTestData1;
    public static List<Long> lsTestData2;



    public static int[] voltageValuesStore = new int[3600];
    public static int[] voltageValuesStoreA = new int[3600];
    public static int[] voltageValuesStoreB = new int[3600];
    public static int[] voltageValuesStoreC = new int[3600];

    public static int[] electricValuesStore = new int[3600];
    public static int[] electricValuesStoreA = new int[3600];
    public static int[] electricValuesStoreB = new int[3600];
    public static int[] electricValuesStoreC = new int[3600];



    public static void record(int newValueVoltageA, int newValueVoltageB,int newValueVoltageC, int newValueEletricA,int newValueEletricB,int newValueEletricC)
    {



        Log.d(TAG,"1:"+newValueVoltageA+"  2:"+newValueVoltageB+"  3:"+newValueVoltageC+ "  4:"+newValueEletricA+ "  5:"+newValueEletricB+"  6:"+newValueEletricC);

        NewTotalDataEntity entity =  NewTotalDataEntity.build(newValueEletricA,newValueEletricB,newValueEletricC,newValueVoltageA,newValueVoltageB,newValueVoltageC);
        entity.save();

       int newValueV = (newValueVoltageA + newValueVoltageB +newValueVoltageC) / 3;
       int newValueE = (newValueEletricA + newValueEletricB + newValueEletricC)/ 3;


        System.arraycopy(voltageValuesStore,0,voltageValuesStore,1,voltageValuesStore.length-1);
        voltageValuesStore[0] = newValueV;

        System.arraycopy(electricValuesStore,0,electricValuesStore,1,electricValuesStore.length-1);
        electricValuesStore[0] = newValueE;

        System.arraycopy(voltageValuesStoreA,0,voltageValuesStoreA,1,voltageValuesStoreA.length-1);
        voltageValuesStoreA[0] = newValueVoltageA;

        System.arraycopy(voltageValuesStoreB,0,voltageValuesStoreB,1,voltageValuesStoreB.length-1);
        voltageValuesStoreB[0] = newValueVoltageB;

        System.arraycopy(voltageValuesStoreC,0,voltageValuesStoreC,1,voltageValuesStoreC.length-1);
        voltageValuesStoreC[0] = newValueVoltageC;

        System.arraycopy(electricValuesStoreA,0,electricValuesStoreA,1,electricValuesStoreA.length-1);
        electricValuesStoreA[0] = newValueEletricA;

        System.arraycopy(electricValuesStoreB,0,electricValuesStoreB,1,electricValuesStoreB.length-1);
        electricValuesStoreB[0] = newValueEletricB;


        System.arraycopy(electricValuesStoreC,0,electricValuesStoreC,1,electricValuesStoreC.length-1);
        electricValuesStoreC[0] = newValueEletricC;
    }




    public static void readTotal(int dayCount) {
        List<String> ls = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < dayCount; i++) {
            calendar.add(Calendar.DAY_OF_MONTH, (0-i));
            ls.add( formatter.format(calendar.getTime()));
        }

        voltageValues = new int[dayCount];
        electricValues = new int[dayCount];

        for( int i = 0;i<dayCount;i++ )
        {
            int dataIndex = dayCount -i -1;
            String currentDay = ls.get(i);
            List<TotalDataEntity> lsFind =  LitePal.where("ymd = ? ", currentDay).find(TotalDataEntity.class);
            Log.d("TOTALTOTAL","找到条数:"+lsFind.size());
            if(lsFind != null && lsFind.size()>0)
            {
                voltageValues[dataIndex] =lsFind.get(0).getVoltageAvgValue();
                electricValues[dataIndex] = lsFind.get(0).getElectricAvgValue();
            }
        }
    }

    private static DateFormat formatter = new SimpleDateFormat("yyyyMMdd");


    private static long lastTotalTime = 0;
    private static int totalSecond = 10000;
    static int count = 10;

    public static void totalSecond( int count ) {

        int[] arrayV = new int[count];
        int[] arrayE = new int[count];
        int[] arrayEA = new int[count];
        int[] arrayEB = new int[count];
        int[] arrayEC = new int[count];

      //  Log.d(TAG,"total,count="+count);
        for(int i =0;i<count;i++)
        {
            int index = count -1 -i;
            arrayV[i] = voltageValuesStore[index];
            arrayE[i] = electricValuesStore[index];
            arrayEA[i] = electricValuesStoreA[index];
            arrayEB[i] = electricValuesStoreB[index];
            arrayEC[i] = electricValuesStoreC[index];

           // Log.d(TAG,"EE:"+arrayE[i]);
        }

        EventBus.getDefault().post(new TotalMainDataChangedEvent(arrayV, arrayE,arrayEA,arrayEB,arrayEC));

    }


    public static void totalSecond( int count ,int second) {

        totalSecond(count,second,2);

    }

    public static void totalSecond( int count ,int second,int sendTo) {

        int[] arrayV = new int[count];
        int[] arrayE = new int[count];
        int[] arrayEA = new int[count];
        int[] arrayEB = new int[count];
        int[] arrayEC = new int[count];

        //  Log.d(TAG,"total,count="+count);
        for(int i =0;i<count;i++)
        {
            int minuteAvgValueV = 0;
            int minuteAvgValueE = 0;
            int eletricValueA = 0;
            int eletricValueB = 0;
            int eletricValueC = 0;

            int currentVCount = 0;
            int currentECount = 0;
            int currentEACount = 0;
            int currentEBCount = 0;
            int currentECCount = 0;

            for(int j = 0;j< second;j++)
            {
                int index = (count -1-i)*second+j;
                int currentV = voltageValuesStore[index];
                int currentE = electricValuesStore[index];
                int currentEA = electricValuesStoreA[index];
                int currentEB = electricValuesStoreB[index];
                int currentEC = electricValuesStoreC[index];

                if(currentV > 0)
                {
                    currentVCount++;
                    minuteAvgValueV+=currentV;
                }
                if(currentE > 0)
                {
                    currentECount++;
                    minuteAvgValueE+=currentE;
                }
                if(currentEA > 0)
                {
                    currentEACount++;
                    eletricValueA += currentEA;
                }
                if(currentEB > 0)
                {
                    currentEBCount++;
                    eletricValueB += currentEB;
                }
                if(currentEC > 0)
                {
                    currentECCount++;
                    eletricValueC += currentEC;
                }
            }
            if(currentVCount > 0)
            {
                minuteAvgValueV = minuteAvgValueV / currentVCount;
            }
            if(currentECount > 0){
                minuteAvgValueE = minuteAvgValueE / currentECount;
            }
            if(currentEACount > 0)
            {
                eletricValueA = eletricValueA / currentEACount;
            }
            if(currentEBCount > 0){
                eletricValueB = eletricValueB / currentEBCount;
            }
            if(currentECCount > 0)
            {
                eletricValueC = eletricValueC / currentECCount;
            }


            arrayV[i] = minuteAvgValueV;
            arrayE[i] = minuteAvgValueE;
            arrayEA[i] = eletricValueA;
            arrayEB[i] = eletricValueB;
            arrayEC[i] = eletricValueC;
            Log.d(TAG,"V:"+arrayV[i]+"  EA:"+arrayEA[i]+"  EB:"+arrayEB[i]+"  EC:"+arrayEC[i]);
        }
        if(sendTo == 0) {
            EventBus.getDefault().post(new TotalMainDataChangedEvent(arrayV, arrayE, arrayEA, arrayEB, arrayEC));
        }
        else if(sendTo == 1) {
            EventBus.getDefault().post(new TotalPopDataChangedEvent(arrayV, arrayE, arrayEA, arrayEB, arrayEC));
        }
    }


    public static void Test(int dayCount)
    {

        new Thread(new Runnable() {
            @Override
            public void run() {
                int index =0;
                int allIndex = 0;
                SystemClock.sleep(2000);
                while(true) {


                    if(testReady) {

                        int count = 10;
                        int[] data1 = new int[count];
                        int[] data2 = new int[count];
                        index = allIndex;
                        for (int i = 0; i < count; i++) {
                            if (lsTestData1.size() > index) {
                                data1[i] = Integer.parseInt(lsTestData1.get(index).toString());
                                data2[i] = Integer.parseInt(lsTestData2.get(index).toString());
                            } else {
                                index = 0;
                                data1[i] = Integer.parseInt(lsTestData1.get(index).toString());
                                data2[i] = Integer.parseInt(lsTestData2.get(index).toString());
                            }
                            index++;
                        }
                        //EventBus.getDefault().post(new TotalMainDataChangedEvent(data1, data2));
                    }
                    allIndex++;
                    if(allIndex >= lsTestData1.size())
                    {
                        allIndex = 0;
                    }
                    if(allIndex > 2) {
                        SystemClock.sleep(60000);
                    }
                    else{
                        SystemClock.sleep(1000);
                    }
                }
            }
        }).start();

    }

    static boolean testReady = false;
    public static void test2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                lsTestData1 = new ArrayList<>();
                lsTestData2 = new ArrayList<>();
                int sampleRate = 10; // 采样率
                int durationInSeconds = 3; // 信号长度（秒）
                int numSines = 3; // 正弦波的数量
                double[] wave1 = generateRegularWave(sampleRate, durationInSeconds, numSines);
                //double[] wave2 = generateRegularWave(sampleRate, durationInSeconds, numSines);
                for(int i =0;i< wave1.length;i++)
                {
                    int index1 = i;
                    int index2 = wave1.length - i -1;
                    lsTestData1.add(Math.round(wave1[index1] * 100));
                    lsTestData2.add(Math.round(wave1[index2] * 100));
                }

                testReady = true;

            }
        }).start();

        // 打印波形数据
       // System.out.println(Arrays.toString(wave));

    }

    public static double[] generateRegularWave(int sampleRate, int durationInSeconds, int numSines) {
        int numSamples = sampleRate * durationInSeconds;
        double[] wave = new double[numSamples];

        double frequencyStep = 1.0 / durationInSeconds;

        for (int i = 0; i < numSamples; i++) {
            double time = i / (double)sampleRate;
            double sum = 0.0;

            for (int j = 1; j <= numSines; j++) {
                double frequency = j * frequencyStep;
                sum += Math.sin(2 * Math.PI * frequency * time) * 0.5;
            }

            wave[i] = sum;
        }

        return wave;
    }



}
